var MaterialClassifyForm = function () {
  var template = /* html */ `
    <div class="material-classify-form" v-loading.fullscreen.lock="loading">
      <div>
        <el-form label-width="auto" size="medium">
          <template v-for="field in classifyLevel">
          <el-form-item :label="i18n.getMsg('bomBusiness.materialClassify.classifyLevel.'+field)" :key="'classifyLevel_'+field">
            <el-input v-if="type==='add'" v-model="classifyForm[field]" class="form-input"></el-input>
            <span v-else>{{classifyForm[field]}}</span>
          </el-form-item>
          </template>

          <el-form-item label="状态">
            <el-select v-model="classifyForm.status" class="form-input">
              <el-option v-for="item in statusList" :key="item.value" :value="item.value" :label="item.label"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div style="text-align:center">
        <el-button size="medium" type="primary" @click="saveClassify">
          <span v-if="type==='add'">新增</span>
          <span v-else>修改</span>
        </el-button>
      </div>
    </div>
  `;

  return {
    props: {
      type: {
        type: String,
        default: 'add'
      },
      classify: {
        type: Object,
        default: function () {
          return null;
        }
      }
    },

    data: function () {
      var classifyForm;
      if (this.type === 'add') {
        classifyForm = new MaterialClassifyModel();
      } else {
        classifyForm = new MaterialClassifyModel(this.classify);
      }
      return {
        loading: false,
        classifyForm: classifyForm,
        classifyLevel: MaterialClassifyModel.classifyLevel,
        statusList: i18n.getDropDown('bomBusiness.materialClassify.status', [1, 0])
      };
    },

    methods: {
      saveClassify: function () {
        var classifyForm = this.classifyForm;
        if (this.type === 'add') {
          this.addClassify(classifyForm);
        } else {
          this.updateClassify(classifyForm);
        }
      },

      addClassify: function (classifyForm) {
        var code = classifyForm.checkValid();
        if (code !== '') {
          var msg = i18n.getMsg(`bomBusiness.materialClassify.error.${code}`);
          this.$alert(msg, '信息');
          return;
        }

        var postData = classifyForm.getObject();
        this.loading = true;
        var self = this;
        ajaxRequest('createOrderForBomOrder/saveLevelRank', postData).then(function () {
          self.loading = false;
          self.$alert('新增层级配置成功', '信息', {
            callback: function () {
              self.$emit('save-success');
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      },

      updateClassify: function (classifyForm) {
        this.loading = true;
        var self = this;
        ajaxRequest('createOrderForBomOrder/updateLevelRank', {
          id: classifyForm.id,
          status: classifyForm.status
        }).then(function () {
          self.loading = false;
          self.$alert('修改层级配置成功', '信息', {
            callback: function () {
              self.$emit('save-success');
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    template: template
  };
}();
